package network.e2e;

import java.io.IOException;

import network.link.FrameInputStream;

/**
 * Reader for FrameInputStream
 */

public class FrameReader {
  
  private FrameInputStream frame_input_stream_;
  
  /**
   * Constructs a frame reader that reads from a frame input stream
   * @requires frame_input_stream not null
   * @param frame_input_stream from which read
   */
  public FrameReader(FrameInputStream frame_input_stream) {
    assert (frame_input_stream != null);
    frame_input_stream_ = frame_input_stream;
  }
  
  /**
   * Reads a string
   * @return the read string or null if end of stream reached
   * @throws IOException 
   */
  public String readString() throws IOException {
    byte[] ret = frame_input_stream_.read();
    if (ret == null) {
      return null;
    }
    return new String(ret);
  }
  
  /**
   * Reads a long
   * @return the long read or null if end of stream reached
   * @throws IOException
   */
  public Long readLong() throws IOException {
    String str = readString();
    if (str == null) {
      return null;
    }
    return Long.valueOf(str);
  }
  
  /**
   * Reads an int
   * @return the int read or null if end of stream reached
   * @throws IOException
   */
  public Integer readInt() throws IOException {
    Long value = readLong();
    if (value == null) {
      return null;
    }
    return value.intValue();
  }
  
  /**
   * Closes the reader
   */
  public void close() {
    frame_input_stream_.close();
  }
  
}
